﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Security.Cryptography;
using ManageBanking.Models;
using System.Text;
namespace ManageBanking.Areas.Admin.Models
{
    public class ManageUserModels
    {
        private OnlineBankingDataContext olbk = new OnlineBankingDataContext();

        public static string EncryptString(string Message, string Passphrase)
        {
            byte[] Results;
            System.Text.UTF8Encoding UTF8 = new System.Text.UTF8Encoding();
            // Buoc 1: Bam chuoi su dung MD5
            MD5CryptoServiceProvider HashProvider = new MD5CryptoServiceProvider();
            byte[] TDESKey = HashProvider.ComputeHash(UTF8.GetBytes(Passphrase));
            // Step 2. Tao doi tuong TripleDESCryptoServiceProvider moi
            TripleDESCryptoServiceProvider TDESAlgorithm = new TripleDESCryptoServiceProvider();
            // Step 3. Cai dat bo ma hoa
            TDESAlgorithm.Key = TDESKey;
            TDESAlgorithm.Mode = CipherMode.ECB;
            TDESAlgorithm.Padding = PaddingMode.PKCS7;
            // Step 4. Convert chuoi (Message) thanh dang byte[]
            byte[] DataToEncrypt = UTF8.GetBytes(Message);
            // Step 5. Ma hoa chuoi
            try
            {
                ICryptoTransform Encryptor = TDESAlgorithm.CreateEncryptor();
                Results = Encryptor.TransformFinalBlock(DataToEncrypt, 0, DataToEncrypt.Length);
            }
            finally
            {
                // Xoa moi thong tin ve Triple DES va HashProvider de dam bao an toan
                TDESAlgorithm.Clear();
                HashProvider.Clear();
            }
            // Step 6. Tra ve chuoi da ma hoa bang thuat toan Base64
            return Convert.ToBase64String(Results);
        }
        public void AddAcc(int TypeUser,string username, string password, string name, string identificard, string sex, DateTime birth, string address, string Email, string PlaceBirth, string job, string phase,int idadmin)
        {
            Username ac = new Username();
            string phase1 = getMD5(phase.Trim());
            string pass_encrypt = EncryptString(password.Trim(), phase1.Trim());
            ac.Password = pass_encrypt.Trim();
            ac.Username1 = username.Trim();
            ac.SecondaryPass = phase1.Trim();
            ac.Name = name.Trim();
            ac.IdentityCard = identificard.Trim();
            ac.Sex = sex.Trim();
            ac.DateBirth = birth;
            ac.Address = address.Trim();
            ac.Email = Email.Trim();
            ac.PlaceBirth = PlaceBirth.Trim();
            ac.Job = job.Trim();
            ac.IdTypeUser = TypeUser;
            
            ac.DateCreate = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd"));

            ac.Idadmin = idadmin;
            ac.Lock = false;
            olbk.Usernames.InsertOnSubmit(ac);
            olbk.SubmitChanges();

        }
        public bool DeleteAccount(int id)
        {
            bool kq = true;
            Username ac = olbk.Usernames.Where(a => a.IdUsername == id).First();
            try
            {
                olbk.Usernames.DeleteOnSubmit(ac);
                olbk.SubmitChanges();
            }
            catch
            {
                return kq = false;
            }

            return kq;
        
        }
        public ManageBanking.Areas.Admin.Models.Username InformationAcc(int id)
        {
            return olbk.Usernames.Where(a => a.IdUsername == id).First();
            
        }
        public ManageBanking.Areas.Admin.Models.Username TakeUserAdmin(int id)
        {
            OnlineBankingDataContext olbk = new OnlineBankingDataContext();
            return olbk.Usernames.Where(a => a.IdUsername == id).First();
        }
        public bool Update(int iduserad,int idusername, string lockuser)
        {
            bool kq = true;
            try
            {
                
                Username un = olbk.Usernames.Where(u => u.IdUsername == idusername).First();
                un.Lock = Convert.ToBoolean(Convert.ToBoolean(lockuser));
                un.Idadmin = iduserad;
                olbk.SubmitChanges();
                kq = true;
            }
            catch
            {
                kq = false;
            }
            return kq;
        }

        public bool Resolve(int idrequest, string lockuser,int idtype,int idadmin,int idusername )
        {
            bool kq = true;
            string pass="";
            MailModels mm = new MailModels();
            try
            {
                
                    RequestAdmin un = olbk.RequestAdmins.Where(u => u.IdRequest == idrequest).First();
                    un.IdAdmin = idadmin;
                    un.Solved = Convert.ToBoolean(Convert.ToBoolean(lockuser));
                    Username us = olbk.Usernames.Where(a => a.IdUsername == idusername).First();
                    mm.SentMail(us.Email, idtype, out pass);
                    if (idtype == 1)
                    {
                        ChangePassword(idusername, pass);
                    
                    }
                    if (idtype == 2)
                    {
                        us.Lock = false;
                    
                    }
                    olbk.SubmitChanges();
                    
                    kq = true;
               
            }
            catch
            {
                kq = false;
            }
            return kq;
        }
        public void ChangePassword(int idaccount, string newpassword)
        {
            Username ac = olbk.Usernames.Where(a => a.IdUsername==idaccount).First();
            ac.Password = EncryptString(newpassword, ac.SecondaryPass);
            olbk.SubmitChanges();
        }
        public string getMD5(String str)
        {
            MD5CryptoServiceProvider mdcsp = new MD5CryptoServiceProvider();
            byte[] b = System.Text.Encoding.UTF8.GetBytes(str);
            b = mdcsp.ComputeHash(b);
            StringBuilder s = new StringBuilder();
            foreach (byte by in b)
            {
                s.Append(by.ToString("x2").ToLower());
            }
            return s.ToString();
        }
    }
}
